#include "StdMobile.h"
#include "pika_hal_mobile.h"

char* StdMobile_get_imei(PikaObj *self, int sim_id){
    char imei[24] = {0};
    int err = pika_hal_mobile_get_imei(sim_id, imei, sizeof(imei));
    return obj_cacheStr(self, imei);
}
char* StdMobile_get_imsi(PikaObj *self, int sim_id){
    char imsi[24] = {0};
    int err = pika_hal_mobile_get_imsi(sim_id, imsi, sizeof(imsi));
    return obj_cacheStr(self, imsi);
}
char* StdMobile_get_iccid(PikaObj *self, int sim_id){
    char iccid[24] = {0};
    int err = pika_hal_mobile_get_iccid(sim_id, iccid, sizeof(iccid));
    return obj_cacheStr(self, iccid);
}
char* StdMobile_get_number(PikaObj *self, int sim_id){
    char number[24] = {0};
    int err = pika_hal_mobile_get_iccid(sim_id, number, sizeof(number));
    return obj_cacheStr(self, number);
}
int StdMobile_get_sim_id(PikaObj *self){
    return pika_hal_mobile_get_sim_id();
}
int StdMobile_get_sim_status(PikaObj *self){
    return pika_hal_mobile_get_sim_status();
}
void StdMobile_set_rrc_time(PikaObj *self, int sec){
    pika_hal_mobile_set_rrc_time(sec);
}


int StdMobile_set_apn(PikaObj *self, int sim_id, int cid, char* apn_name, char* user_name, char* password, int ip_type, int protocol){
    return pika_hal_mobile_set_apn(sim_id, cid, apn_name, user_name, password, ip_type, protocol);
}
int StdMobile_active_apn(PikaObj *self, int sim_id, int cid, pika_bool enable){
    return pika_hal_mobile_active_apn(sim_id, cid, enable);
}

char* StdMobile_get_apn(PikaObj *self, int sim_id, int cid){
    char apn_name[64] = {0};
    pika_hal_mobile_get_apn(sim_id, cid, apn_name, sizeof(apn_name));
    return obj_cacheStr(self, apn_name);
}

int StdMobile_get_flymode(PikaObj *self, int sim_id){
    return pika_hal_mobile_get_flymode(sim_id);
}
void StdMobile_set_flymode(PikaObj *self, int sim_id, pika_bool enable){
    pika_hal_mobile_set_flymode(sim_id, enable);
}

void StdMobile_set_ipv6(PikaObj *self, pika_bool enable){
    pika_hal_mobile_set_ipv6(enable);
}
pika_bool StdMobile_get_ipv6(PikaObj *self){
    return pika_hal_mobile_get_ipv6();
}

void StdMobile_set_sync_time(PikaObj *self, pika_bool enable){
    pika_hal_mobile_set_sync_time(enable);
}

int StdMobile_get_register_status(PikaObj *self){
    return pika_hal_mobile_get_register_status();
}
int StdMobile_get_cell_info_async(PikaObj *self, int timeout){
    return pika_hal_mobile_get_cell_info_async(timeout);
}
#if 0
//测试列表内存泄露用
PikaObj* StdMobile_get_cell_info(PikaObj *self){
    
    PikaList* A = New_PikaList();
    PikaList* B = New_PikaList();

    pikaList_append(A, arg_newStr("LTE"));
    pikaList_append(B, arg_newInt(1));
    pikaList_append(B, arg_newInt(2));
    pikaList_append(B, arg_newInt(3));
    pikaList_append(A, arg_newObj(B));
    return A;
}
#endif
#if 1
PikaObj* StdMobile_get_cell_info(PikaObj *self){
    pika_hal_mobile_cell_info all_cell_info;
    int err = pika_hal_mobile_get_cell_info(&all_cell_info);
    
    PikaList* all_cell_info_list = New_PikaList();
    PikaList* _service_cell_info_list = NULL; //临时变量
    PikaList* _neighbor_cell_info_list = NULL; //要多次使用的临时变量
    // 如果处于LTE网络
    switch (all_cell_info.network_type)
    {
        case PIKA_HAL_MOBILE_NETWORK_GSM:{
            break;
        }
        case PIKA_HAL_MOBILE_NETWORK_LTE:{
            pika_hal_mobile_lte_service_cell_info *lte_service_info = &all_cell_info.lte_cell_info.service_info;
            //生成主服务小区的列表
            _service_cell_info_list = New_PikaTuple();
            pikaList_append(_service_cell_info_list, arg_newStr("servingcell"));
            pikaList_append(_service_cell_info_list, arg_newStr("LTE"));
            pikaList_append(_service_cell_info_list, arg_newInt(lte_service_info->cid));
            pikaList_append(_service_cell_info_list, arg_newInt(lte_service_info->mcc));
            pikaList_append(_service_cell_info_list, arg_newInt(lte_service_info->mnc));
            pikaList_append(_service_cell_info_list, arg_newInt(lte_service_info->tac));
            pikaList_append(_service_cell_info_list, arg_newInt(lte_service_info->pci));
            pikaList_append(_service_cell_info_list, arg_newInt(lte_service_info->earfcn));
            pikaList_append(_service_cell_info_list, arg_newInt(lte_service_info->rssi));
            pikaList_append(_service_cell_info_list, arg_newInt(lte_service_info->rsrp));
            pikaList_append(_service_cell_info_list, arg_newInt(lte_service_info->rsrq));
            pikaList_append(_service_cell_info_list, arg_newInt(lte_service_info->snr));
            pikaList_append(_service_cell_info_list, arg_newInt(lte_service_info->is_tdd));
            pikaList_append(_service_cell_info_list, arg_newInt(lte_service_info->band));
            pikaList_append(_service_cell_info_list, arg_newInt(lte_service_info->ulbandwidth));
            pikaList_append(_service_cell_info_list, arg_newInt(lte_service_info->dlbandwidth));

            //装填之后把该列表添加到总列表中去
            pikaList_append(all_cell_info_list, arg_newObj(_service_cell_info_list));

            pika_hal_mobile_lte_neighbor_cell_info *lte_neighbor_info;
            int neighbor_num = all_cell_info.lte_cell_info.neighbor_info_num;
            // __platform_printf("neighbor_num: %d", neighbor_num);
            //循环生成邻小区的列表
            for(int i = 0; i < neighbor_num; i++){
                lte_neighbor_info = &all_cell_info.lte_cell_info.neighbor_infos[i];
                _neighbor_cell_info_list = New_PikaTuple();
                pikaList_append(_neighbor_cell_info_list, arg_newStr("neighborcell"));
                pikaList_append(_neighbor_cell_info_list, arg_newStr("LTE"));
                pikaList_append(_neighbor_cell_info_list, arg_newInt(lte_neighbor_info->cid));
                pikaList_append(_neighbor_cell_info_list, arg_newInt(lte_neighbor_info->mcc));
                pikaList_append(_neighbor_cell_info_list, arg_newInt(lte_neighbor_info->mnc));
                pikaList_append(_neighbor_cell_info_list, arg_newInt(lte_neighbor_info->tac));
                pikaList_append(_neighbor_cell_info_list, arg_newInt(lte_neighbor_info->pci));
                pikaList_append(_neighbor_cell_info_list, arg_newInt(lte_neighbor_info->earfcn));
                pikaList_append(_neighbor_cell_info_list, arg_newInt(lte_neighbor_info->bandwidth));
                pikaList_append(_neighbor_cell_info_list, arg_newInt(lte_neighbor_info->celltype));
                pikaList_append(_neighbor_cell_info_list, arg_newInt(lte_neighbor_info->rsrp));
                pikaList_append(_neighbor_cell_info_list, arg_newInt(lte_neighbor_info->rsrq));
                pikaList_append(_neighbor_cell_info_list, arg_newInt(lte_neighbor_info->snr));
                pikaList_append(_neighbor_cell_info_list, arg_newInt(lte_neighbor_info->rssi));

                //装填之后把该列表添加到总列表中去
                pikaList_append(all_cell_info_list, arg_newObj(_neighbor_cell_info_list));
            }
            break;
        }
        default:
            break;
    }
    return all_cell_info_list;
}

#endif

int StdMobile_get_csq(PikaObj *self){
    int csq = 99;
    pika_hal_mobile_get_csq(&csq);
    return csq;
}
int StdMobile_get_rssi(PikaObj *self){
    int rssi = -113;
    pika_hal_mobile_get_rssi(&rssi);
}


